const app = getApp();
var util = require('../../../utils/util.js')
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    lists: wx.getStorageSync('lists-cdd') || []
  },
  onShow: function () {
    var list1 = wx.getStorageSync('lists-cdd') || [];
    this.setData({
      lists: list1
    });
  },
  // 新开一局
  newGame: function (event) {
    var record1 = wx.getStorageSync('last-cdd');
    var record2 = {
      player:
        [{
          avatar: 0,
          name: '阿甲',
          score: 0
        }, {
          avatar: 0,
          name: '阿乙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丙',
          score: 0
        }, {
          avatar: 0,
          name: '阿丁',
          score: 0
        }],
      time: util.formatTime(new Date()),
      winner: 0,
      panshu: 0,
      newscore: [0, 0, 0, 0],
      defen: []
    };
    var list1 = wx.getStorageSync('lists-cdd') || [];
    console.log('lists=' + list1);
    list1.unshift(record1);
    wx.setStorageSync('lists-cdd', list1);
    wx.setStorageSync('last-cdd', record2);
    wx.navigateTo({
      url: '../index/index'
    })
  },
  // 继续
  goon: function (event) {
    wx.showModal({
      title: '提示',
      content: '您确定继续这一局吗？当前局会加入历史记录中！',
      // showCancel: false,
      cancelText: '取消',
      confirmText: '确认',
      success: function (res) {
        if (res.confirm) {
          var uid = event.currentTarget.dataset.lishi
          console.log("获取到的历史=" + uid);
          var list1 = wx.getStorageSync('lists-cdd') || [];
          var record2 = list1[uid];
          // console.log('lists=' + list1);
          var record1 = wx.getStorageSync('last-cdd');
          if (record1.panshu > 0) {
            list1.unshift(record1);
            wx.setStorageSync('lists-cdd', list1);
          }
          wx.setStorageSync('last-cdd', record2);
          // this.setData({
          //   lists: list1
          // });
          wx.navigateTo({
            url: '../index/index'
          })
        } else if (res.cancel) {

        }
      }
    })

  },

  // 删除
  del: function (event) {
    let that = this;
    wx.showModal({
      title: '提示',
      content: '您确定删除该记录？',
      // showCancel: false,
      cancelText: '取消',
      confirmText: '确认',
      success: function (res) {
        if (res.confirm) {
          var uid = event.currentTarget.dataset.lishi
          console.log("获取到的历史=" + uid);
          var list1 = wx.getStorageSync('lists-cdd') || [];
          // console.log('lists=' + list1);
          list1.splice(uid, 1);
          wx.setStorageSync('lists-cdd', list1);
          that.setData({
            lists: list1
          });
        } else if (res.cancel) {

        }
      }
    })

  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  gridchange: function (e) {
    this.setData({
      gridCol: e.detail.value
    });
  },
  gridswitch: function (e) {
    this.setData({
      gridBorder: e.detail.value
    });
  },
  menuBorder: function (e) {
    this.setData({
      menuBorder: e.detail.value
    });
  },
  menuArrow: function (e) {
    this.setData({
      menuArrow: e.detail.value
    });
  },
  menuCard: function (e) {
    this.setData({
      menuCard: e.detail.value
    });
  },
  switchSex: function (e) {
    this.setData({
      skin: e.detail.value
    });
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },

  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
})